joApps.TextEditor = {

	id: 'TextEditor',

	menubar: {
		'TextEditor': {
			'About TextEditor': 'joApps.TextEditor.about();'
		},

		'File': {
			'New': 'joApps.TextEditor.newFile();',
			'Open...': 'joApps.TextEditor.open();',
			'Save': 'joApps.TextEditor.save();',
			'Save As...': 'joApps.TextEditor.saveAs();',
		},
	},


	onload: function(){
		return joCore.IO.load('css', 'apps/TextEditor/TextEditor.app.css');
	},
	
	start: function(){
		this.newFile();
	},

	about: function(){
		joCore.UI.dialog({
			x: 100,
			y: 100,
			width: 300,
			height: 300,
			message: 'Text Editor ver 0.0.1<br />Created by Dinos Project Team',
			title: 'About This TextEditor',
			application: this.id,
		});
	},

	newFile: function(){
		var t = new joApps.TextEditor.window({
			width: 300,
			height: 300,
			application: this.id
		});
	},

	open: function(){
		var fp = new joFilePicker({
			mode: 'Open',
		});
		fp.show().next(function(path){
			if(!path) return;
			joApps.TextEditor.openFile(path);
		});
	},
	
	openFile: function(aPath){
		var t = new joApps.TextEditor.window({
			width: 300,
			height: 300,
			application: joApps.TextEditor.id,
			path: aPath
		});
	
		t.display.editor.appendChild(document.createTextNode(joFileManager.getFile(aPath).getAttribute('data')));
	},

	save: function(){
		var win = joAppManager.getFrontWindow(this.id);
		var fileContent = win.owner.display.editor.value;
			
		//書き込む
		joCore.IO.write(fileContent, win.param.path, true);
			
		//いろいろな設定
		joFileManager.getFile(path).setAttribute('onCommand', 'joApps.TextEditor.openFile("' + win.param.path + '");');
	},
	
	saveAs: function(){
		//保存先を決める
		var fp = new joFilePicker({
			mode: 'Save',
		});
		fp.show().next(function(path){
			if(!path) return;
		
			var win = joAppManager.getFrontWindow(this.id);
			var fileContent = win.owner.display.editor.value;
			
			//書き込む
			joCore.IO.write(fileContent, path, false);
			
			//いろいろな設定
			joFileManager.getFile(path).setAttribute('onCommand', 'joApps.TextEditor.openFile("' + path + '");');
			win.owner.setTitle(joFileManager.getFileNameFromPath(path));
		});
	},

};


joApps.TextEditor.window = function(param){
	this.param = param;
	this.display = {};

	this.init();
	this.init_self();
}

joApps.TextEditor.window.prototype = {

	init_self: function(){
		this.display.editor = joCore.Node.createNode('textarea', null, this.display.content);
		if(this.param.path) this.setTitle(joFileManager.getFileNameFromPath(this.param.path));
	},
	
};
joApps.TextEditor.window.prototype.__proto__ = joWindow.prototype;